package net.easyjava.base.utils.http.juhe;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import net.easyjava.base.utils.http.juhe.bean.JokeBean;
import net.easyjava.base.utils.http.juhe.bean.ResultBean;
import net.easyjava.base.utils.http.juhe.constants.JuheConstants;
import net.easyjava.base.utils.http.juhe.constants.KeyConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wxnacy on 16/1/30.
 */
public class JuheJokeUtils extends JuheApiUtils {
    private static Logger logger = LoggerFactory.getLogger(JuheJokeUtils.class);
    public static final String URL_LIST_PARAMS = "?sort=%s&time=%s&page=%s&pagesize=%s&key=%s";

    public static final String URL_TEXT_LIST = "http://japi.juhe.cn/joke/content/list.from"+URL_LIST_PARAMS;
    public static final String URL_TEXT_TEXT = "http://japi.juhe.cn/joke/content/text.from"+URL_LIST_PARAMS;

    public static final String URL_IMG_LIST = "http://japi.juhe.cn/joke/img/list.from"+URL_LIST_PARAMS;
    public static final String URL_IMG_TEXT = "http://japi.juhe.cn/joke/img/text.from"+URL_LIST_PARAMS;


    /**
     * 获取最新图片列表
     * @param page
     * @param pageSize
     * @return
     */
    public static List<JokeBean> getJokeImgList(int page,int pageSize){
        return getJokeList(URL_IMG_TEXT,JuheConstants.JOKE_TYPE_IMG,null,null,page,pageSize);
    }

    /**
     * 获取图片列表
     * @param sort
     * @param time
     * @param page
     * @param pageSize
     * @return
     */
    public static List<JokeBean> getJokeImgList(String sort,String time,int page,int pageSize){
        return getJokeList(URL_IMG_LIST,JuheConstants.JOKE_TYPE_IMG,sort,time,page,pageSize);
    }

    /**
     * 获取最新的笑话列表
     * @param page
     * @param pageSize
     * @return
     */
    public static List<JokeBean> getJokeTextList(int page,int pageSize){
        return getJokeList(URL_TEXT_TEXT,JuheConstants.JOKE_TYPE_TEXT,null,null,page,pageSize);
    }

    /**
     * 获取文字笑话列表
     * @param sort
     * @param time
     * @param page
     * @param pageSize
     * @return
     */
    public static List<JokeBean> getJokeTextList(String sort,String time,int page,int pageSize){
        return getJokeList(URL_TEXT_LIST,JuheConstants.JOKE_TYPE_TEXT,sort,time,page,pageSize);
    }

    /**
     * 通过地址获取笑话列表
     * @param type
     * @param sort
     * @param time
     * @param page
     * @param pageSize
     * @return
     */
    protected static List<JokeBean> getJokeList(String url,int type,String sort,String time,int page,int pageSize){
        url = String.format(url,sort,time,page,pageSize, KeyConstants.KEY_JOKE);
        List<JokeBean> list = new ArrayList<JokeBean>();

        ResultBean resultBean = getResult(url);
        if(resultBean == null){
            return list;
        }
        if(resultBean.isSuccess()){
            JSONObject data = JSONObject.parseObject(resultBean.getResult());

            JSONArray array =  data.getJSONArray("data");
            for(Object obj:array){
                JSONObject json = (JSONObject) obj;
                JokeBean jokeBean = JSONObject.parseObject(json.toJSONString(),JokeBean.class);
                jokeBean.setType(type);
                list.add(jokeBean);
            }
            return list;
        }else{
            return list;
        }

    }


    public static void main(String[] args) {
        System.out.println("getJokeImgList(1,10) = " + getJokeImgList("desc","1453973631",1, 10));
    }

}
